<template>
    <div class='family'>
        <el-drawer :title="title" visible @close="$emit('close'); $emit('load')" size="1200px" :wrapperClosable="false">
            <div class="add-main box" v-loading="loading">
                <el-row :gutter="10" class="mb8" v-if="edit">
                    <el-col :span="20">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleShow = 'add'"
                            :disabled="!userId">
                            新增紧急联系人</el-button>
                    </el-col>
                    <el-col :span="4">
                        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                    </el-col>
                </el-row>
                <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight>
                    <el-table-column label="序号" align="center" prop="id" />
                    <el-table-column label="姓名" align="center" prop="name" />
                    <el-table-column label="年龄" align="center" prop="age" />
                    <!-- <el-table-column label="性别" align="center">
                        <template slot-scope="scope">
                            <span>
                                {{ selectDictLabel(seX, scope.row.sex) }}
                            </span>
                        </template>
                    </el-table-column> -->
                    <!-- <el-table-column label="身份证号" align="center" prop="id_card" /> -->
                    <el-table-column label="联系电话" align="center" prop="contact" />
                    <el-table-column label="联系地址" align="center" prop="detailed" >
                        <template slot-scope="scope">
                            <span>
                                {{ scope.row.addres_province }}{{ scope.row.addres_city }}{{ scope.row.addres_area }}{{ scope.row.addres_detailed }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="与老人关系" align="center">
                        <template slot-scope="scope">
                            <span>
                                {{ selectDictLabel(elderRelative, scope.row.with) }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column v-if="edit" label="操作" align="center" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit"
                                @click="form = { ...form, ...JSON.parse(JSON.stringify(scope.row)) }; handleShow = 'edit'">
                                编辑
                            </el-button>
                            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">
                                删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="pageCount > 0" :pageCount="pageCount" :page.sync="queryParams.page"
                    :limit.sync="queryParams.limit" @pagination="getList" />
                <el-col class="row-bg" justify="end" :span="24" v-if="edit">
                    <el-button type="primary" size="mini" @click="$emit('close')" :disabled="loading">
                        保存</el-button>
                </el-col>
                <el-dialog :title="handleShow == 'add' ? '新增' : '编辑'" visible v-if="handleShow" @close="close()"
                    width="800px" append-to-body :close-on-click-modal="false">
                    <el-row>
                        <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-loading="formLoading">
                            <el-col :span="12">
                                <el-form-item label="姓名" prop="name">
                                    <el-input style="width:215px;" v-model="form.name" placeholder="请输入姓名" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="年龄" prop="age">
                                    <el-input style="width:215px;" v-model="form.age" @keyup.native="form.age=handleNum(form.age)" placeholder="请输入年龄" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系方式" prop="contact">
                                    <el-input style="width:215px;" v-model="form.contact" placeholder="请输入联系方式" />
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="与老人关系:" prop="with">
                                    <el-select v-model="form.with" placeholder="请选择" class="handle-select mr10">
                                        <el-option v-for="dict in elderRelative" :key="dict.dictValue"
                                            :value="dict.dictValue" :label="dict.dictLabel">
                                            {{ dict.dictLabel }}
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <!-- <el-col :span="12">
                                <el-form-item label="性别:" prop="sex">
                                    <el-radio-group v-model="form.sex" class="mr10">
                                        <el-radio v-for="dict in seX" :key="dict.dictValue" :label="Number(dict.dictValue)">
                                            {{ dict.dictLabel }}
                                        </el-radio>z
                                    </el-radio-group>
                                </el-form-item>
                            </el-col> -->
                            <el-col :span="12">
                                <el-form-item label="地址省份:" prop="addres_province">
                                    <el-select v-if="edit" v-model="form.addres_province" placeholder="请选择省份"
                                        @change="getCurrentCity" clearable>
                                        <el-option v-for="dict in areaData" :key="dict.code" :label="dict.name"
                                            :value="dict.name" />
                                    </el-select>
                                    <span v-else>
                                        {{ form.addres_province }}
                                    </span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="城市:" prop="addres_city">
                                    <el-select v-if="edit" v-model="form.addres_city" placeholder="请选择城市"
                                        @change="getCurrentArea" filterable clearable>
                                        <el-option v-for="dict in currentCityList" :key="dict.code" :label="dict.name"
                                            :value="dict.name" />
                                    </el-select>
                                    <span v-else>
                                        {{ form.addres_city }}
                                    </span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="区/县：" prop="addres_area">
                                    <el-select v-if="edit" v-model="form.addres_area" placeholder="请选择区/县" filterable
                                        clearable>
                                        <el-option v-for="dict in currentAreaList" :key="dict.code" :label="dict.name"
                                            :value="dict.name" />
                                    </el-select>
                                    <span v-else>
                                        {{ form.addres_area }}
                                    </span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="详细地址：" prop="addres_detailed">
                                    <el-input v-if="edit" v-model="form.addres_detailed" placeholder="请输入详细地址" />
                                    <span v-else>
                                        {{ form.addres_detailed }}
                                    </span>
                                </el-form-item>
                            </el-col>
                            <!-- <el-col :span="12">
                                <el-form-item label="联系地址" prop="detailed">
                                    <el-input style="width:215px;" v-model="form.detailed" placeholder="请输入联系地址" />
                                </el-form-item>
                            </el-col> -->
                        </el-form>
                    </el-row>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="close()">取 消</el-button>
                        <el-button type="primary" @click="submit" :disabled="formLoading">确 定</el-button>
                    </span>
                </el-dialog>
            </div>
        </el-drawer>
    </div>
</template>
<script>
import { getContactsListsAll, setContactsInfo, delContactsInfo } from "@/api/senior/emergency.js"
import { isvalidatemobile, cardid } from "@/utils/validate";
import { seX, elderRelative } from "@/api/dictionary"
import throttle from 'lodash.throttle'
export default {
    name: 'family',
    props: {
        title: String,
        userId: {
            type: String,
        }
    },
    data() {
        const ismobile = (rule, value, callback) => {
            if (isvalidatemobile(value)[0]) {
                callback(new Error(isvalidatemobile(value)[1]));
            } else {
                callback();
            }
        };
        const cardId = (rule, value, callback) => {
            if (cardid(value)[0]) {
                callback(new Error(cardid(value)[1]));
            } else {
                callback();
            }
        };
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            seX: seX,
            elderRelative: elderRelative,
            queryParams: {
                page: 1,
                limit: 20,
            },
            tableList: [],
            handleShow: "",
            formLoading: false,
            currentCityList: [],
            currentAreaList: [],
            form: {
                id: null,
                uid: this.userId,
                name: null,
            },
            rules: {
                name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
                contact: [
                    { required: true, message: '请输入联系方式', trigger: 'blur' },
                    { validator: ismobile, trigger: "blur" }
                ],
                // id_card: [
                //     { validator: cardId, trigger: "blur" },
                // ],
                with: [
                    { required: true, message: "请选择与老人关系", trigger: "blur" },
                ],
                addres_province: [
                    { required: true, message: "请选择地址省份", trigger: "blur" },
                ],
                addres_city: [
                    { required: true, message: "请选择城市", trigger: "blur" },
                ],
                addres_area: [
                    { required: true, message: "请选择区/县", trigger: "blur" },
                ],
                addres_detailed: [
                    { required: true, message: "请输入详细地址", trigger: "blur" },
                ],

            },
            edit: true,
        }
    },
    watch: {
        "userId"(val) {
            if (val) {
                this.$set(this.form, "uid", val)
                this.getList()
            }
        }
    },
    created() {
       console.log(this.userId, "用户id")
        this.getList()
    },
    methods: {
        getList() {
            this.loading = true;
            getContactsListsAll({ uid: this.userId }).then(res => {
                this.tableList = res.data
                // this.pageCount = res.data.pageCount;
                this.loading = false;
            });
        },
        getCurrentCity(val) {
            if (val) {
                this.$set(this.form, "addres_city", null)
                this.$set(this.form, "addres_area", null)
                this.currentAreaList = []
                this.currentCityList = this.areaData.find(item => item.name == val).cityList;
            }
        },
        getCurrentArea(val) {
            if (val) {
                this.$set(this.form, "addres_area", null)
                this.currentAreaList = this.currentCityList.find(item => item.name == val).areaList;
            }
        },
        close() {
            this.form = {}
            this.$set(this.form, "uid", this.userId)
            this.handleShow = ""
        },
        handleDelete(row) {
            this.$confirm('是否确认删除该家属?删除后将不可恢复!', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delContactsInfo({ contacts_id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    this.form.contacts_id = this.form.id
                    setContactsInfo(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.getList();
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),
    }
}
</script>

<style lang='scss' scoped>
.row-bg {
    margin-top: 30px;
    display: flex;
    justify-content: flex-end;
}

/deep/.top-right-btn {
    button:first-child {
        display: none;
    }
}
</style>